<template>
  <div class="common-layout linear-gradient">
    <el-container>
      <el-header class="header-container">
        <div class="headerTitle">查询条件</div>
        <div class="header-content">
          <el-form :model="headerSearchForm" label-width="auto" class="header-search-form">
            <el-form-item label="工艺规则描述">
              <el-input v-model="headerSearchForm.processRuleDescription" placeholder="请输入工艺规则描述" />
            </el-form-item>
            <el-form-item label="工艺规则变量">
              <el-input v-model="headerSearchForm.ruleName" placeholder="请输入工艺规则变量" />
            </el-form-item>
            <el-form-item label="初始变量名">
              <el-input v-model="headerSearchForm.l1VariableName" placeholder="请输入初始变量名" />
            </el-form-item>
          </el-form>
          <div class="header-buttons">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleAdd">添加</el-button>
          </div>
        </div>
      </el-header>
      <el-main>
        <div class="table-container-main">
          <el-table
            :data="tableData"
            :header-cell-style="{ background: '#eef1f6', color: '#606266', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }"
          >
            <el-table-column prop="serialNumber" label="序号" fixed="left">
              <template #default="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="productionLine" label="产线" />
            <el-table-column prop="calcResultVariable" label="计算变量名" />
            <el-table-column prop="dicVariableNameCalc" label="dicVariableNameCalc" />
            <el-table-column prop="ruleName" label="工艺规则变量" />
            <el-table-column prop="processRuleDescription" label="工艺规则描述" />
            <el-table-column prop="l1VariableName" label="初始变量名" />
            <el-table-column prop="ruleEditor" label="规则编辑器" />
            <el-table-column prop="dataBegin" label="数据开始" />
            <el-table-column prop="dataEnd" label="数据结束" />
            <el-table-column label="操作" width="200" fixed="right">
              <template #default="scope">
                <el-button type="warning" @click="handleEdit(scope.row)">修改</el-button>
                <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-main>
    </el-container>
  </div>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="handleClose" ref="dialogRef" @closed="cancel">
    <el-form :model="dialogForm" label-width="auto">
      <el-form-item label="产线">
        <el-input v-model="dialogForm.productionLine" placeholder="请输入变量" />
      </el-form-item>
      <el-form-item label="计算变量名">
        <el-input v-model="dialogForm.calcResultVariable" placeholder="请输入变量" />
      </el-form-item>
      <el-form-item label="dicVariableNameCalc">
        <el-input v-model="dialogForm.dicVariableNameCalc" placeholder="请输入变量" />
      </el-form-item>
      <el-form-item label="工艺规则变量">
        <el-input v-model="dialogForm.ruleName" placeholder="请输入变量" />
      </el-form-item>
      <el-form-item label="工艺规则描述">
        <el-input v-model="dialogForm.processRuleDescription" placeholder="请输入工艺规则描述" />
      </el-form-item>
      <el-form-item label="初始变量名">
        <el-input v-model="dialogForm.l1VariableName" placeholder="请输入初始变量名" />
      </el-form-item>
      <el-form-item label="规则编辑器">
        <el-input v-model="dialogForm.ruleEditor" placeholder="请输入规则编辑器" />
      </el-form-item>
      <el-form-item label="数据开始">
        <el-input v-model="dialogForm.dataBegin" placeholder="请输入数据开始" />
      </el-form-item>
      <el-form-item label="数据结束">
        <el-input v-model="dialogForm.dataEnd" placeholder="请输入数据结束" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="dialogFormSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts">
  import { ref, reactive, onMounted, onUnmounted, nextTick, defineComponent } from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import './steelCoilServiceModelCommonStyle.less';
  import { api } from '/@/api/steelCoilServiceModel/processRuleEditing';

  interface SearchForm {
    processRuleDescription: string;
    ruleName: string;
    l1VariableName: string;
  }

  interface dialogForm {
    indocno: Number;
    productionLine: string;
    calcResultVariable: string;
    processRuleDescription: string;
    dicVariableNameCalc: string;
    l1VariableName: string;
    ruleEditor: string;
    ruleName: string;
    toc: string;
    dataBegin: string; //头尾数据处理标识
    dataEnd: string; //头尾数据处理标识
    dataBegin1: string; // 备用字段
    headMove: string; //去掉曲线头部米数后计算特征值
    tailMove: string; // 去掉曲线尾部米数后计算特征值
  }

  export default defineComponent({
    name: 'ProcessRuleEditing',
    setup() {
      // 表单数据
      const headerSearchForm = reactive<SearchForm>({
        processRuleDescription: '',
        ruleName: '',
        l1VariableName: '',
      });

      // 表格数据
      const tableData = ref([
        {
          indocno: 0,
          productionLine: '',
          calcResultVariable: '',
          processRuleDescription: '',
          dicVariableNameCalc: '',
          l1VariableName: '',
          ruleEditor: '',
          ruleName: '',
          toc: '',
          dataBegin: '', //头尾数据处理标识
          dataEnd: '', //头尾数据处理标识
          dataBegin1: '', // 备用字段
          headMove: '', //去掉曲线头部米数后计算特征值
          tailMove: '', // 去掉曲线尾部米数后计算特征值
        },
      ]);

      let dialogVisible = ref(false);
      let dialogTitle = ref('');
      const dialogForm = reactive<dialogForm>({
        indocno: 0,
        productionLine: '',
        calcResultVariable: '',
        processRuleDescription: '',
        dicVariableNameCalc: '',
        l1VariableName: '',
        ruleEditor: '',
        ruleName: '',
        toc: '',
        dataBegin: '', //头尾数据处理标识
        dataEnd: '', //头尾数据处理标识
        dataBegin1: '', // 备用字段
        headMove: '', //去掉曲线头部米数后计算特征值
        tailMove: '', // 去掉曲线尾部米数后计算特征值
      });
      const dialogRef = ref();

      // 添加方法
      const handleAdd = () => {
        dialogVisible.value = true;
        dialogTitle.value = '新增';
      };

      // 编辑方法
      const handleEdit = (row: any) => {
        dialogVisible.value = true;
        dialogTitle.value = '修改';
        Object.assign(dialogForm, { ...row });
      };

      const cancel = () => {
        dialogVisible.value = false;
        Object.assign(dialogForm, {
          indocno: 0,
          productionLine: '',
          calcResultVariable: '',
          processRuleDescription: '',
          dicVariableNameCalc: '',
          l1VariableName: '',
          ruleEditor: '',
          ruleName: '',
          toc: '',
          dataBegin: '', //头尾数据处理标识
          dataEnd: '', //头尾数据处理标识
          dataBegin1: '', // 备用字段
          headMove: '', //去掉曲线头部米数后计算特征值
          tailMove: '', // 去掉曲线尾部米数后计算特征值
        });
      };

      const dialogFormSubmit = async () => {
        try {
          if (dialogTitle.value === '修改') {
            await api.update(dialogForm);
          } else {
            await api.add(dialogForm);
          }
        } catch (error) {
          ElMessage.error('操作数据失败');
        } finally {
          ElMessage.success('操作成功');
          Object.assign(dialogForm, {
            indocno: 0,
            productionLine: '',
            calcResultVariable: '',
            processRuleDescription: '',
            dicVariableNameCalc: '',
            l1VariableName: '',
            ruleEditor: '',
            ruleName: '',
            toc: '',
            dataBegin: '', //头尾数据处理标识
            dataEnd: '', //头尾数据处理标识
            dataBegin1: '', // 备用字段
            headMove: '', //去掉曲线头部米数后计算特征值
            tailMove: '', // 去掉曲线尾部米数后计算特征值
          });
          dialogVisible.value = false;
          fetchTableData();
        }
      };

      const handleClose = (done: () => void) => {
        ElMessageBox.confirm('Are you sure to close this dialog?')
          .then(() => {
            done();
          })
          .catch(() => {
            // catch error
          });
      };

      // 分页数据
      const pagination = reactive({
        currentPage: 1,
        pageSize: 10,
        total: tableData.value.length,
      });

      // 窗口大小变化处理
      const handleResize = () => {
        nextTick(() => {
          // 这里可以添加表格自适应高度的逻辑
        });
      };

      // 获取表格数据
      const fetchTableData = async () => {
        try {
          const param = {
            page: {
              pageIndex: pagination.currentPage,
              pageSize: pagination.pageSize,
            },
            dto: headerSearchForm,
          };
          const res = await api.page(param);
          tableData.value = res.data.data;
          pagination.currentPage = res.data.data.current;
          // pagination.pageSize = res.data.data.size;
          pagination.total = res.data.count;
        } catch (error) {
          ElMessage.error('获取数据失败');
        }
      };

      // 查询方法
      const handleSearch = () => {
        pagination.currentPage = 1;
        fetchTableData();
      };

      // 重置方法
      const handleReset = () => {
        (Object.keys(headerSearchForm) as Array<keyof SearchForm>).forEach((key) => {
          headerSearchForm[key] = '';
        });
        handleSearch();
      };

      // 删除方法
      const handleDelete = async (row: any) => {
        try {
          await api.delete(row.indocno);
        } catch (error) {
          ElMessage.error('删除失败');
        } finally {
          fetchTableData();
        }
      };

      // 分页大小变化
      const handleSizeChange = (val: number) => {
        pagination.pageSize = val;
        fetchTableData();
      };

      // 页码变化
      const handleCurrentChange = (val: number) => {
        pagination.currentPage = val;
        fetchTableData();
      };

      // 生命周期钩子
      onMounted(() => {
        fetchTableData();
        window.addEventListener('resize', handleResize);
      });

      onUnmounted(() => {
        window.removeEventListener('resize', handleResize);
      });

      return {
        headerSearchForm,
        tableData,
        pagination,
        handleSearch,
        handleReset,
        handleAdd,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        dialogVisible,
        dialogTitle,
        dialogForm,
        dialogFormSubmit,
        dialogRef,
        cancel,
        handleClose,
      };
    },
  });
</script>
